package zut.edu.cn.concertticketmanagementsystem.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import zut.edu.cn.concertticketmanagementsystem.dto.OrderDTO;
import zut.edu.cn.concertticketmanagementsystem.entity.OrderForm;
import zut.edu.cn.concertticketmanagementsystem.entity.User;
import zut.edu.cn.concertticketmanagementsystem.service.BuyerService;
import zut.edu.cn.concertticketmanagementsystem.service.OrderFormService;
import zut.edu.cn.concertticketmanagementsystem.service.UserService;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: heroma
 * Date: 6/23/19
 * Time: 2:18 PM
 * Description: No Description
 */
@Controller
@Slf4j
public class OrderFormController {
    @Autowired
    private OrderFormService orderFormService;
    @Autowired
    private UserService userService;
    @Autowired
    private BuyerService buyerService;

    @RequestMapping("/")
    public String index() {
        return "redirect:/list";
    }

    @RequestMapping("/buyer")
    public String listByName( String buyerName, Model model) {
        List<OrderForm> list = buyerService.findOrderByName(buyerName);
        List<User>users=new ArrayList<>();
        users = userService.findUserByusername(buyerName);
        if(list.size()==0){
            model.addAttribute("users", users);
            return "order/listerror";
        }
        else {
            buyerName = users.get(0).getUsername();
            model.addAttribute("orderlist", list);
            return "order/buyer";
        }
    }
    @RequestMapping("/list")
    public String list(String name,Model model) {
        List<User>users=userService.findUserByusername(name);
        if(users.size()==0)
            return "login";
        else {
            List<OrderForm> list = orderFormService.list();
            model.addAttribute("order", list);
            return "order/list";
        }
    }
    @RequestMapping("/cancel")
    public String cancel(String id,Model model) {
        OrderDTO orderDTO = orderFormService.findOne(id);
        if (orderDTO.getOrderStatus() == 0) {
            orderFormService.cancel(orderDTO);
            model.addAttribute("dto", orderDTO);
            return "order/cancelok";
        } else {
            model.addAttribute("orderDTO", orderDTO);
            return "order/error";
        }


    }
    @RequestMapping("/finish")
    public String finish(String id,Model model) {
        OrderDTO orderDTO = orderFormService.findOne(id);
        if (orderDTO.getOrderStatus()==0){
            orderFormService.finish(orderDTO);
            model.addAttribute("finishdto", orderDTO);
            return "order/finishok";
        }
        else{
            model.addAttribute("orderDTO", orderDTO);
            return "order/error";
        }
    }
    @RequestMapping("/delete")
    public String delete(String id,Model model) {
        OrderDTO orderDTO = orderFormService.findOne(id);
        model.addAttribute("deleteOrderDto", orderDTO);
        if(orderDTO.getOrderId()!=null) {
            orderFormService.deleteByOrderId(orderDTO.getOrderId());
            return "order/deleteok";
        }

        else{
            return "order/deleteerror";
        }
    }
    @RequestMapping("/paid")
    public String paid(String id,Model model) {
        OrderDTO orderDTO = orderFormService.findOne(id);
        if(orderDTO.getPayStatus()==0){
            orderFormService.paid(orderDTO);
            model.addAttribute("paiddto", orderDTO);
            return "order/paidok";
        }
        else{
            model.addAttribute("orderDto", orderDTO);
            return "order/paiderror";
        }
    }
    @GetMapping("/detail")
    public String detail(String id, Model model) {
        OrderDTO orderDTO= orderFormService.findOne(id);
        model.addAttribute("orderdto", orderDTO);
        return "order/detail";
    }

}

